/*
 * @Author: wurangkun
 * @Date: 2024-05-28 20:31:18
 * @LastEditTime: 2024-07-24 18:10:09
 * @LastEditors: wurangkun
 * @FilePath: \my-app\app\(tabs)\record.tsx
 * @Description: 
 */
import { router } from "expo-router";
import { Alert, Button, DeviceEventEmitter, Linking, Modal, Platform, Text, View } from "react-native";
import { useCameraPermissions } from 'expo-camera';
import { useEffect } from "react";
import { to } from "@/hooks/routerHooks";
import * as MediaLibrary from "expo-media-library"

export default function Reocrd() {
  const [permission, requestCameraPermission] = useCameraPermissions();
  const [permissionResponse, requestPermission] = MediaLibrary.usePermissions();

  useEffect(() => {
    const sub = DeviceEventEmitter.addListener('refresh_tab', () => {
      console.log('refresh_tab')
    })
    return () => {
      sub.remove()
    }
  }, [])

  const toCamera = async () => {
    // MediaLibrary.requestPermissionsAsync()
    const res = await requestCameraPermission()
    if (permissionResponse?.status !== 'granted') {
      await requestPermission();
    }
    console.log('res', res)
    if (res.granted) {
      to("/pages/camera")
    } else {
      Alert.alert("没有权限", "请到设置中开启相机权限", [
        {
          text: "取消",
          onPress: () => console.log("Cancel Pressed"),
          style: "cancel"
        },
        {
          text: "确定", onPress: () => Linking.openSettings()
        }
      ])
    }
  }

  const toProfile = () => {
    to("/pages/userInfo/profile");
  }
  return (
    <View
      style={{
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
      }}
    >
      <Text>Reocrd</Text>
      <Button title="拍照" onPress={toCamera}></Button>
      <Button title="toProfile" onPress={toProfile}></Button>

    </View>
  );
}
